<template>
    <div class="post_add">
        <el-form ref="form" :model="form">
            <el-form-item label="">
                <el-input v-model="form.name" placeholder="请输入文章标题"></el-input>
            </el-form-item>
        </el-form>
        <textarea id="editor" placeholder="Balabala" autofocus></textarea>
    </div>
</template>

<script>
    import Simditor from 'simditor'
    import {log,buildParams,sendMessage} from '@/assets/js/util.js'
    export default{
        props:['bus'],
        name: 'post_add',
        data () {
            return {
                msg: 'post_add',
                editor:{},
                form:{
                    name:'',
                }
            }
        },
        methods:{
            handleAddPost:function (data) {
                data.title = this.form.name
                data.content = this.editor.getValue()
                if(!data.title){
                    sendMessage(language.post_add.no_title,'warning',this)
                    return;
                }
                if(!data.content){
                    sendMessage(language.post_add.no_content,'warning',this)
                    return;
                }
                if(!data.categorys){
                    sendMessage(language.post_add.no_category,'warning',this)
                    return;
                }
                if(data.tags.length == 0){
                    sendMessage(language.post_add.no_tags,'warning',this)
                    return;
                }
                if(!data.img){
                    sendMessage(language.post_add.no_img,'warning',this)
                }
                var url = server_url+''
                var params = []
                params[0] = 'data='+JSON.parse(data)
                this.$http.jsonp(url, {}, {
                    headers: {},
                    emulateJSON: true
                }).then(function(response) {
                    this.categorys = response.data.data[0]
                    this.tags = response.data.data[1]
                    log(response)
                }, function(response) {
                    log(response)
                });
            }
        },
        mounted:function () {
            this.editor = new Simditor({
                textarea: $('#editor')
            });
            this.bus.$on('sendAttrs',this.handleAddPost)
        }
    }
</script>

<style>
    .post_add{
        padding: 15px;
    }
</style>